import Vue from 'vue'
import VueRouter from 'vue-router'
import { __getUserAs } from 'common/js/user'

const OrderList = () => import('components/order-list/order-list')
const OrderCreate = () => import('components/order-create/order-create')
const OrderDetail = () => import('components/order-detail/order-detail')
const OrderReceive = () => import('components/order-receive/order-receive')
const Login = () => import('components/login/login')
const Register = () => import('components/register/register')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/list'
  },
  {
    path: '/list',
    name: 'list',
    component: OrderList,
    children: [
      {
        name: 'detail',
        path: 'detail',
        component: OrderDetail
      },
      {
        name: 'create',
        path: 'create',
        component: OrderCreate
      },
      {
        name: 'receive',
        path: 'receive',
        component: OrderReceive
      }
    ]
  },
  {
    name: 'login',
    path: '/login',
    component: Login,
    children: [
      {
        name: 'register',
        path: 'register',
        component: Register
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: '/ipackage/',
  routes
})

router.beforeEach(async (to, from, next) => {
  if (to.name !== 'login') {
    await __getUserAs()
  }
  next()
})

export default router
